<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <!--<script type="text/javascript" src="./js/xadmin.js"></script>-->
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>-->
    <!--<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>-->
    <!--<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>-->
    <!--[endif]-->
    <script type="text/javascript" src="../../../common/js/public.js"></script>
    <script type="text/javascript" src="../../../common/js/publicUtils.js"></script>
</head>
<body>
<div class="layui-fluid" id="flowDiv" onload="onLoad()">
    <div class="zTreeBackground left" style="float: left;width:250px">
        <div class="titlediv">
            <p style="text-align:right;background-color:white;line-height: 30px">
                物资字典信息<i class="layui-icon layui-icon-refresh" style="margin-left: 25%" onclick="refreshRoot()"></i>&ensp;&ensp;
            </p>

        </div>
        <div class="searchdiv" >
            <div class="layui-input-inline" style="width:100%">
                <input type="text" id="search" name="keyword" autocomplete="off" class="layui-input" placeholder="请输入名称"/>
                <i class="layui-icon layui-icon-search" style="position: absolute;top:8px;right: 8px;" onclick="searchByName()"></i>
            </div>
        </div>
        <div class="ztreediv" id="ztreediv" style="overflow:auto">
            <div class="ztreeuldiv">
                <ul id="treeStructure" class="ztree" style="overflow:auto"></ul>
            </div>
        </div>
    </div>
    <div class="zTreeBackground right" style="float: left; padding-left:1%">
        <div class="layui-form-item" style="width:99%;margin-bottom:-10px;text-align:right;">
            <div class="layui-inline">
                <label class="layui-form-label">物资名称</label>
                <div class="layui-input-block">
                    <input type="text" id="name1" lay-verify="required|phone" autocomplete="off" class="layui-input" placeholder="请输入物资名称"/>
                    <i class="layui-icon layui-icon-search" style="position: absolute;top:8px;right: 8px;" onclick="easysearch()"></i>
                </div>
            </div>
            <button type="button" style="margin-left:12px;margin-top: -12px;" class="layui-btn layui-btn-normal" onclick="moresearch()"><i class="layui-icon layui-icon-search"></i>高级搜索</button>
        </div>
        <div class="search_float_div" style="position:absolute;z-index:999;width:70%;background-color: #f1f1f1;padding-top: 20px;text-align:left;display:none">
            <div class="layui-form-item">
                <div class="layui-inline" style="width:400px;">
                    <label class="layui-form-label">物资编码</label>
                    <div class="layui-input-block">
                        <input type="text" id="code" lay-verify="required|phone" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline" style="width:400px;">
                    <label class="layui-form-label">物资名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="name" lay-verify="required|phone" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline" style="width:400px;">
                    <label class="layui-form-label">规格型号</label>
                    <div class="layui-input-block">
                        <input type="text" id="specs" lay-verify="required|phone" autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" style="float: left;margin-left: 12px" class="layui-btn layui-btn-normal" onclick="searchTable()"><i class="layui-icon layui-icon-search"></i>查询</button>
                    <button type="button" style="float: left;margin-left: 12px" class="layui-btn layui-btn-normal" onclick="resetSearchTable()"><i class="layui-icon layui-icon-search"></i>重置</button>
                    <button type="button" class="layui-btn layui-btn-primary" onclick="canclesearch()">取消</button>
                </div>
            </div>
        </div>
        <table class="layui-hide" id="datatable"></table>
        <script type="text/html" id="toolbar">
            <!--添加按钮-->
        </script>

        <!--<div class="submitdiv">-->
            <!--<button type="button" class="layui-btn layui-btn-normal" onclick="saveMenu(1)"><i class="layui-icon">&#xe608;</i>保存</button>-->
            <!--<button type="button" class="layui-btn layui-btn-normal" onclick="saveMenu()"><i class="layui-icon">&#xe608;</i>应用</button>-->
            <!--<button type="button" class="layui-btn layui-btn-normal" onclick="cancle()"><i class="layui-icon">&#xe608;</i>取消</button>-->
        <!--</div>-->
    </div>
    <!--<div class="layui-row layui-col-space15">-->

        <!--<div class="layui-col-md4 treetool" id="flowDiv" onload="onLoad()">-->

        <!--</div>-->
        <!--<div class="layui-col-md8">-->
            <!--<input type="button" value="获取选中项" onclick="getSelectNode()"/>-->
            <!--<input type="button" value="获取check项" onclick="getCheckNode()"/>-->
            <!--<input type="button" value="刷新选中项" onclick="refreshSelectNode()"/>-->
        <!--</div>-->
    <!--</div>-->
</div>
</body>
<SCRIPT type="text/javascript">
    var zTreeObj,form,cols,url,tableObj,table,level=0,treeNodeId=-1,selecNode,count=1;

    layui.use(["form"],function () {
        form = layui.form;
    })
    $(document).ready(function(){
        loadbutton("toolbar");//加载按钮loadbutton(id)，id为button容器id，不传默认为buttongroup
        loadTree();
    });
    window.onload = getContentSize;
    window.onresize = getContentSize;

    /**
     * 页面自适应
     */
    function getContentSize() {
        var wh = document.documentElement.clientHeight;
        var eh = 30;
        ch = (wh - eh) + "px";
        document.getElementById( "flowDiv" ).style.height = ch;
        document.getElementById( "ztreediv" ).style.height = (wh - eh - 50)+"px";
        document.body.style.height = (wh)+"px";
        $(".zTreeBackground.right").css("width",($(document).width()-350)+'px');//自适应表格处理
    }
    /**
     * 左侧树设置
     * @type {{check: {enable: boolean}, data: {simpleData: {enable: boolean, idKey: string, pIdKey: string, isParent: string, rootPid: number}}, callback: {beforeClick: zTreeBeforeClick, onClick: zTreeOnClick}}}
     */
    var setting = {
        edit:{
            enable: false,
        },
        async: {
            enable: true,
            url: publicUrl+"sysMaterialClassification/selectSysMaterialClassificationByPid",
            headers: {"Authorization":getData("Authorization")},
            dataFilter: formatedata,
            otherParam:{//传入查询参数
                //注意，要传入动态变化的参数必须用return的方式
                "name":function(){return $("#search").val()
                }
            },

            autoParam: ["code"]
        },
        data: {
            simpleData: {
                enable: true,
                idKey:"code",
                pIdKey:"parentId",
                rootPid:-1
            },
            key: {
                isParent:"parent",
                icon:"xIcon"
            }
        },
        callback: {
            beforeClick: zTreeBeforeClick,
            onClick: zTreeOnClick,
            onAsyncSuccess: selectAppointNode //异步加载数据完成后的回调函数
        }
    };
    /**
     * @Author bxf
     * @Description 搜索框按钮
     * @Date 11:20 2019/7/17
     * @Param 
     * @return 
     **/
    function searchByName(){
        count = 1;
        loadTree();
    }

    /**
     * @Author bxf
     * @Description 刷新左边树
     * @Date 11:20 2019/7/17
     * @Param
     * @return
     **/
    function refreshRoot(){
        count = 1;
        $("#search").val("");
        loadTree();
    }

    /**
     * 点击前验证
     * @param treeId
     * @param treeNode
     * @param clickFlag
     * @returns {boolean}
     */
    function zTreeBeforeClick(treeId, treeNode, clickFlag) {
        //return (treeNode.id !== menuid);
        return true;
    };
    /**
     * 加载左侧树
     */
    function loadTree(){
        zTreeObj = $.fn.zTree.init($("#treeStructure"), setting, null);
    }
    /**
     * 选中第一个节点
     * @param treeobj
     * @param id
     */
    function selectAppointNode(event, treeId, treeNode, msg) {
        if ($("#treeStructure_1_a").length > 0 && count ==1) {
            $("#treeStructure_1_a").click();
            count++;
        }
    }

    /**
     * 数据拦截器
     * @param treeId
     * @param parentNode
     * @param responseData
     */
    function formatedata(treeId,parentNode,responseData) {
        return formatTreeParseData(treeId,parentNode,responseData);
    }

    /**
     * 点击触发
     * @param event
     * @param treeId
     * @param treeNode
     */
    function zTreeOnClick(event, treeId, treeNode) {
        if(treeNode){
            level = treeNode.level;
            treeNodeId = treeNode.code;
            selecNode = treeNode;
        }
        $("#name,#name1,#code,#specs").val("");
        loadtable();
    };
    /* *
     * @Author bxf
     * @Description 快速查询
     * @Date 10:14 2019/10/29
     * @param null
     * @return 
     */
    function easysearch(){
        loadtable($("#name1").val(),null,null);
    }
    /* *
     * @Author bxf
     * @Description 更多查询
     * @Date 10:15 2019/10/29
     * @param null
     * @return
     */
    function searchTable() {
        loadtable($("#name").val(),$("#code").val(),$("#specs").val());
        moresearch();
    }
    /* *
     * @Author bxf
     * @Description 取消查询
     * @Date 10:16 2019/10/29
     * @param null
     * @return
     */
    function canclesearch() {
        moresearch();
    }
    /* *
     * @Author bxf
     * @Description 重置查询
     * @Date 10:29 2019/10/29
     * @param null
     * @return 
     */
    function resetSearchTable(){
        $("#name,#name1,#code,#specs").val("");
        moresearch();
        loadtable();
    }
    /* *
    * @Author bxf
    * @Description 高级搜索
    * @Date 17:34 2019/8/30
    * @param null
    * @return
    */
    function moresearch(){
        $(".search_float_div").slideToggle();
    }

    function loadtable(name,code,specs) {
        url = publicUrl+'resMaterialLibrary/selectListByPid';
        cols = [[
            {
                type:'checkbox',
                width: 60,
                align: 'center'
            },
            {
                type: 'numbers',
                title: '序号',
                width: 60,
                align: 'center'
            }, {
                field: 'code',
                title: '物资编码',
                width: "14%",
                sort: true,
                align: 'center',
            },{
                field: 'name',
                title: '物资名称',
                sort: true,
                width: "14%",
                align: 'center',
            }, {
                field: 'pname',
                title: '所属分类',
                width: "16%",
                align: 'center',
                templet:function(r){
                    if(selecNode){
                        return selecNode.name;
                    }else{
                        return "物资字典";
                    }
                }
            },{
                field: 'specs',
                title: '规格型号',
                width: "16%",
                align: 'center',
            },{
                field: 'unit',
                title: '单位',
                width: "8%",
                align: 'center',
            },
            {
                field: 'comment',
                title: '备注',
                width: "20%",
                align: 'center',
            }]];
        var width = $(".left-nav").css("width");
        $(".zTreeBackground.right").css("width",($(document).width()-350)+'px');//自适应表格处理
        layui.use(['form','table'], function(){
            form = layui.form;
            table = layui.table;
            tableObj = table.render({
                elem: '#datatable',
                size: 'sm',
                toolbar: '#toolbar',
                even: true, //开启隔行背景
                height: 'full-50',
                headers: {"Authorization":getData("Authorization")},
                method: 'post',
                url: url, //stationId='+window.parent.window.parentId,//8AE7DECA-8E41-4FAB-8B30-9FD1B0B7FA33,78A9E5D8-20FB-4436-BBE8-A25EAC38D056
                where:{
                    category:treeNodeId,
                    name:name,
                    code:code,
                    specs:specs
                },
                parseData: function(res){ //res 即为原始返回的数据
                    return formatTableParseData(res);
                },
                request: {
                    pageName: 'pageIndex' //页码的参数名称，默认：page
                    ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                response: {
                    statusCode: 2000,
                },
                cols: cols,
                page: true
            });

        });
    };
    /**
     * @Author bxf
     * @Description 添加菜单页面
     * @Date 9:51 2019/7/18
     * @Param 
     * @return 
     **/
    function addMateriallibrary(callback){
        var iframeWin ;
        var nodes = zTreeObj.getSelectedNodes()[0];
        layer.open({
            type: 2,
            area: ["630px" , "580px"],
            fix: false,
            //不固定
            maxmin: false,
            shade: 0.3,
            title: "新增物资",
            content: "./addMateriallibrary.html",
            btn: ['应用','确定', '关闭'],
            // 弹层外区域关闭
            shadeClose: false,
            yes: function(index, layero) {
                 var resultdata = iframeWin.contentWindow.submit(level);//应用
                 if(resultdata && resultdata.code==2000){
                     //refreshSelectNode(zTreeObj,nodes.id);
                     pointNode(zTreeObj,nodes.id);
                 }
                 return false;
                 //layer.closeAll();
            },
            btn2: function(index, layero){
            //按钮【按钮二】的回调
                var resultdata = iframeWin.contentWindow.submit(level);//保存
                if(resultdata && resultdata.code==2000){
                    //refreshSelectNode(zTreeObj,nodes.id);
                    pointNode(zTreeObj,nodes.id)
                    layer.closeAll();
                }else{
                    return false;
                }

            //return false 开启该代码可禁止点击该按钮关闭
            },
            btn3: function(index, layero){
                //按钮【按钮三】的回调

                //return false 开启该代码可禁止点击该按钮关闭
            },
            cancel: function(index) {
                return true;
            },
            success: function(layero, index){
                iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.voluate(1,nodes);//添加按钮
            }
        });
    }

    /**
     * @Author bxf
     * @Description 添加菜单页面
     * @Date 9:51 2019/7/18
     * @Param
     * @return
     **/
    function editMateriallibrary(){
        var obj = table.checkStatus('datatable');
        if(!obj || obj.data.length!=1){
            layer.alert("请选择一项信息进行修改！");
            return false;
        }
        var data = obj.data[0]; //idTest 即为基础参数 id 对应的值
        data.pname = selecNode.name;
        var nodes = zTreeObj.getSelectedNodes()[0];
        var iframeWin ;
        layer.open({
            type: 2,
            area: ["630px" , "580px"],
            fix: false,
            //不固定
            maxmin: false,
            shade: 0.3,
            title: "编辑物资",
            content: "./addMateriallibrary.html",
            btn: ['确定', '关闭'],
            // 弹层外区域关闭
            shadeClose: false,
            yes: function(index, layero) {
                var resultdata = iframeWin.contentWindow.updateInfdict(level);
                if(resultdata && resultdata.code==2000){
                    //refreshSelectNode(zTreeObj,nodes.id);
                    pointNode(zTreeObj,nodes.id);
                    layer.closeAll();
                }else {
                    return false;
                }
            },
            cancel: function(index) {
                return true;
            },
            success: function(layero, index){
                iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.voluate(2,data);//添加按钮
            }
        });
    }
    /* *
     * @Author bxf
     * @Description 刷新table
     * @Date 9:47 2019/10/29
     * @param null
     * @return
     */
    function loadTable(){
        var nodes = zTreeObj.getSelectedNodes()[0];
        pointNode(zTreeObj,nodes.id);
    }
    /* *
     * @Author bxf
     * @Description 导入物资列表
     * @Date 8:58 2019/10/29
     * @param null
     * @return
     */
    function uploadMateriallibrary(){
        //获取选中项
        var nodes = zTreeObj.getSelectedNodes()[0];
        if(!nodes){
            layer.alert("请选择一项物资分类！");
            return false;
        }
        var iframeWin ;
        var nodes = zTreeObj.getSelectedNodes()[0];
        layer.open({
            type: 2,
            area: ["500px" , "350px"],
            fix: false,
            //不固定
            maxmin: false,
            shade: 0.3,
            title: "导入物资清单",
            content: "./uploadmateriallibrary.html",
            btn: ['关闭'],
            // 弹层外区域关闭
            shadeClose: false,
            yes: false,
            cancel: function(index) {
                return true;
            },
            success: function(layero, index){
                iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.getdisciplineId(nodes);//添加按钮
            }
        });
    }

    function  deleteMateriallibrary() {
        //获取选中项
        var obj = table.checkStatus('datatable');
        if(!obj || obj.data.length<1){
            layer.alert("请至少选择一项信息！");
            return false;
        }
        var ids = new Array();
        for(var i=0;i<obj.data.length;i++){
            ids.push(obj.data[i].id);
        }
        var nodes = zTreeObj.getSelectedNodes()[0];
        layui.use(['layer'],function(){
            var layer = layui.layer;
            layer.confirm('确定删除？', {
                btn: ['确定', '取消'] //可以无限个按钮
            }, function(){
                var delurl = "resMaterialLibrary/deleteResMaterialLibraryByIds";
                publicAjax (null,delurl,{"ids":ids.join(",")},null,null,function(r){
                    if(r.code==2000){
                        laysuccess("删除成功",null);
                        //refreshSelectNode(zTreeObj,nodes.id);
                        pointNode(zTreeObj,nodes.id);
                    }else{
                        layerror("删除失败",null);
                    }
                })
            }, function(){
                layer.closeAll();

            });

        })
    }


    /**
     * 打开选中指定节点
     * @param treeobj
     * @param id
     */
    function getAppointNode(treeobj,id){
        if(id){
            var node = treeobj.getNodeByParam("id",id);
            treeobj.selectNode(node,true);//指定选中ID的节点
            //treeobj.expandNode(node, true, false);//指定选中ID节点展开
        }
    }



    /**
     * 刷新指定节点
     */
    function refreshSelectNode(obj,id) {
        var node = obj.getNodeByParam("id",id);
        obj.reAsyncChildNodes(node,"refresh");
    }
    /**
     * 选中某个节点
     */
    function pointNode(obj,id){
        var nodeClick = obj.getNodeByParam("id", id);
        obj.selectNode(nodeClick);//选择点
        obj.setting.callback.onClick(null, id, nodeClick);//调用点击事件
    }





</script>
</html>